import re
import string
from channels.channels import Channel

class Lines2Lines(Channel):
    """
    Convert lines to lines by regex.
    """
    required = ['lines']
    send = ['lines']
    
    @staticmethod
    def execute(args={}, meta={}, lines:list=[], **kwargs):
        
        # 停用字符
        stop_chars = r'「」_《》0-9' + string.ascii_letters
        # mdb_seg 分句标识符
        seg_flag = r'！：；‘’“”？。，~、（）\ ' + string.punctuation + string.whitespace

        lines_news = []
        line_limit = args.get('para_limit', [0, 20000])
        for index, line in enumerate(lines):
            cropus, n = re.subn(r'['+ stop_chars +']', '', line)
            segL_res = re.split(r'['+seg_flag+']', cropus)
            
            # bar show
            args.bar_show(index, len(lines))

            for res in segL_res:
                line_len = len(res)
                if line_len >= line_limit[0] and line_len <= line_limit[1]:
                    lines_news.append(res)

        return {
            'args': args,
            'meta': meta,
            'lines': lines_news
        }